<template>
  <div class="sc-list-tip">
    <el-button :loading="refreshLoading" size="mini" @click="handleRefresh">
      刷新
    </el-button>
    <a
      v-if="attrs.buttonGo"
      target="_blank"
      :href="attrs.buttonGo.url"
      class="el-button el-button--primary el-button--mini"
      style="text-decoration: none;"
    >
      {{ attrs.buttonGo.text }}</a>
    <span
      style="margin-left: 10px;"
    >{{ attrs.paras }}
      <a v-if="attrs.buttonLink" target="_blank" :href="attrs.buttonLink.url">{{
        attrs.buttonLink.text
      }}</a></span>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { refreshStart, refreshStop } from '@/utils/refresh'
export default {
  props: {
    attrs: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  computed: {
    ...mapGetters(['refreshLive', 'refreshStock']),
    refreshLoading() {
      return this.attrs.type === '1'
        ? !!this.refreshLive
        : !!this.refreshStock
    }
  },
  mounted() {
    refreshStart(this.refreshConfig())
  },
  beforeDestroy() {
    refreshStop()
  },
  methods: {
    refreshConfig() {
      const baseConfig = {
        live: {
          apiStart: 'live/refresh',
          apiStatus: 'live/refreshStatus',
          storeKey: 'refreshLive',
          storeSet: 'refresh/setLive',
          storeClear: 'refresh/clearLive',
          listBus: 'refreshLiveList'
        },
        stock: {
          apiStart: 'stock/refresh',
          apiStatus: 'stock/refreshStatus',
          storeKey: 'refreshStock',
          storeSet: 'refresh/setStock',
          storeClear: 'refresh/clearStock',
          listBus: 'refreshStockList'
        }
      }
      return this.attrs.type === '1'
        ? baseConfig['live']
        : baseConfig['stock']
    },
    async handleRefresh() {
      const baseConfig = this.refreshConfig()
      /*
         * dayjs().valueOf() :Unix 时间戳 (毫秒)
         * dayjs().unix() : Unix 时间戳 (秒)
         */
      const ts = this.$dayjs().valueOf()
      console.log(ts)
      try {
        const res = await this.$api[baseConfig.apiStart]()
        await this.$store.dispatch(baseConfig.storeSet, res.ts)
      } catch (err) {
        console.error(err)
      }
    }
  }
}
</script>

<style lang="scss" scoped></style>
